/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UpdateSinistroDialog.java
 *
 * Created on 23/11/2011, 16:45:47
 */
package br.com.seguradora.view.dialog;

import br.com.seguradora.exception.InputValidationException;
import br.com.seguradora.exception.PersistenciaException;
import br.com.seguradora.model.Foto;
import br.com.seguradora.model.Sinistro;
import br.com.seguradora.model.Veiculo;
import br.com.seguradora.service.SinistroService;
import br.com.seguradora.view.ErrorsList;
import br.com.seguradora.view.StateEntityWindow;
import br.com.seguradora.view.UserError;
import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.text.ParseException;
import java.util.Iterator;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import javax.swing.SwingWorker;
import um.filteredtext.number.CurrencyNumberAdapter;

/**
 *
 * @author Diego
 */
public class UpdateSinistroDialog extends javax.swing.JDialog {

    private Sinistro sinistro;
    private final static String titleWindow = "Sinistro";
    private StateEntityWindow stateEntity;
    private CurrencyNumberAdapter valorPrejuizoFormat;

    public UpdateSinistroDialog(java.awt.Frame parent, boolean modal, StateEntityWindow stateEntity, Sinistro sinistro) {
        super(parent, modal);
        initComponents();
        this.stateEntity = stateEntity;
        setTitle(titleWindow + stateEntity);
        organizeWindow();
        setSinistro(sinistro);
        setResizable(false);
        setLocationRelativeTo(null);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        codigoLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        codigoVeiculoField = new javax.swing.JTextField();
        veiculoField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        codigoAssociadoField = new javax.swing.JTextField();
        associadoField = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel5 = new javax.swing.JLabel();
        valorPrejuizoField = new javax.swing.JTextField();
        confirmarButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        photographLabel = new javax.swing.JLabel();
        buttonBar1 = new javax.swing.JToolBar();
        buttonBar2 = new javax.swing.JToolBar();
        jScrollPane1 = new javax.swing.JScrollPane();
        observacaoField = new javax.swing.JTextArea();
        cancelarButton = new javax.swing.JButton();
        valorErrorLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        dataField = new org.jdesktop.swingx.JXDatePicker();
        dataErrorLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Código:");

        codigoLabel.setText("0");

        jLabel3.setText("Veículo:");

        codigoVeiculoField.setEditable(false);
        codigoVeiculoField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        codigoVeiculoField.setFocusable(false);

        veiculoField.setEditable(false);
        veiculoField.setFocusable(false);

        jLabel4.setText("Associado:");

        codigoAssociadoField.setEditable(false);
        codigoAssociadoField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        codigoAssociadoField.setFocusable(false);

        associadoField.setEditable(false);
        associadoField.setFocusable(false);

        jLabel5.setText("Valor Prejuízo:");

        confirmarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/accept.png"))); // NOI18N
        confirmarButton.setText("Confirmar");
        confirmarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmarButtonActionPerformed(evt);
            }
        });

        photographLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        photographLabel.setText("Nenhuma imagem selecionada");
        photographLabel.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEtchedBorder(), javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        photographLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        photographLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        buttonBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        buttonBar1.setRollover(true);

        buttonBar2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        buttonBar2.setRollover(true);

        observacaoField.setColumns(20);
        observacaoField.setLineWrap(true);
        observacaoField.setRows(5);
        observacaoField.setToolTipText("Observações");
        observacaoField.setWrapStyleWord(true);
        jScrollPane1.setViewportView(observacaoField);

        cancelarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/cross.png"))); // NOI18N
        cancelarButton.setText("Cancelar");
        cancelarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarButtonActionPerformed(evt);
            }
        });

        valorErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        jLabel2.setText("Data:");

        dataErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(codigoLabel)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(codigoAssociadoField, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(associadoField))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(codigoVeiculoField, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(veiculoField, javax.swing.GroupLayout.PREFERRED_SIZE, 404, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(valorPrejuizoField, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(valorErrorLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(dataField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dataErrorLabel)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 985, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(buttonBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(photographLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 857, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(63, 63, 63)
                .addComponent(confirmarButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelarButton)
                .addContainerGap(722, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 985, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(codigoLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2)
                                .addComponent(dataField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(dataErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(codigoVeiculoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(veiculoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(codigoAssociadoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(associadoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel5)
                                .addComponent(valorPrejuizoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(valorErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(buttonBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                    .addComponent(buttonBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                    .addComponent(photographLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(confirmarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cancelarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void confirmarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmarButtonActionPerformed
        try {
            validateUserInput();
            setCursor(new Cursor(Cursor.WAIT_CURSOR));
            SinistroService sinistroService = new SinistroService();
            Sinistro sinistro = getSinistro();
            if (getStateEntity() == StateEntityWindow.UPDATE) {
                sinistroService.atualizar(sinistro);
            }
            setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            dispose();
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        } catch (InputValidationException ivex) {
            JOptionPane.showMessageDialog(null, "Erros foram encontrados.", "Mensagem do sistema", JOptionPane.ERROR_MESSAGE);
            ErrorsList errorsList = ivex.getErrorsList();
            for (Iterator<UserError> it = errorsList.iterator(); it.hasNext();) {
                UserError userError = it.next();
                showErrorLabel(userError);
            }
        }
}//GEN-LAST:event_confirmarButtonActionPerformed

    private void cancelarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelarButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            System.out.println(ex);
        }
        //</editor-fold>
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Sinistro sinistro = null;
                try {
                    SinistroService service = new SinistroService();
                    sinistro = service.listarTodos(new int[]{0, 1}).get(0);
                    service.getFotos(sinistro);
                } catch (PersistenciaException pex) {
                    System.out.println(pex.getMessage());
                }
                UpdateSinistroDialog dialog = new UpdateSinistroDialog(new javax.swing.JFrame(), true, StateEntityWindow.UPDATE, sinistro);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField associadoField;
    private javax.swing.JToolBar buttonBar1;
    private javax.swing.JToolBar buttonBar2;
    private javax.swing.JButton cancelarButton;
    private javax.swing.JTextField codigoAssociadoField;
    private javax.swing.JLabel codigoLabel;
    private javax.swing.JTextField codigoVeiculoField;
    private javax.swing.JButton confirmarButton;
    private javax.swing.JLabel dataErrorLabel;
    private org.jdesktop.swingx.JXDatePicker dataField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextArea observacaoField;
    private javax.swing.JLabel photographLabel;
    private javax.swing.JLabel valorErrorLabel;
    private javax.swing.JTextField valorPrejuizoField;
    private javax.swing.JTextField veiculoField;
    // End of variables declaration//GEN-END:variables

    private void organizeWindow() {
        isShowErrorsLabel(false);
        buttonBar1.add(Box.createGlue());
        buttonBar1.add(Box.createGlue());

        buttonBar2.add(Box.createGlue());
        buttonBar2.add(Box.createGlue());

        configTextFieldsDeDinheiro();
    }

    public StateEntityWindow getStateEntity() {
        return stateEntity;
    }

    private void setSinistro(Sinistro sinistro) {
        if (sinistro == null) {
            return;
        }
        this.sinistro = sinistro;
        codigoLabel.setText(String.valueOf(sinistro.getId()));
        dataField.setDate(sinistro.getData());
        
        Veiculo veiculo = sinistro.getVeiculo();
        codigoVeiculoField.setText(String.valueOf(veiculo.getId()));
        veiculoField.setText(veiculo.toString());
        codigoAssociadoField.setText(String.valueOf(veiculo.getAssociado().getId()));
        associadoField.setText(veiculo.getAssociado().getNome());
        
        valorPrejuizoField.setText(valorPrejuizoFormat.getNumberFormat().formatNumber(sinistro.getValorPrejuizo()));
        observacaoField.setText(sinistro.getObservacao());

        loadimages.execute();
        buttonBar1.requestFocus();
    }

    private void configTextFieldsDeDinheiro() {
        valorPrejuizoFormat = new CurrencyNumberAdapter(false);
        valorPrejuizoField.addKeyListener(valorPrejuizoFormat);
        valorPrejuizoField.addFocusListener(valorPrejuizoFormat);
    }
    /**
     * SwingWorker class that loads the images a background thread and calls publish
     * when a new one is ready to be displayed.
     *
     * We use Void as the first SwingWroker param as we do not need to return
     * anything from doInBackground().
     */
    private SwingWorker<Void, ThumbnailAction> loadimages = new SwingWorker<Void, ThumbnailAction>() {

        /**
         * Creates full size and thumbnail versions of the target image files.
         */
        @Override
        protected Void doInBackground() throws Exception {
            for (int i = 0; i < sinistro.getFotosList().size(); i++) {
                ImageIcon icon;
                icon = createImageIcon(sinistro.getFotosList().get(i));

                ThumbnailAction thumbAction = null;
                if (icon != null) {

                    ImageIcon thumbnailIcon = new ImageIcon(getScaledImage(icon.getImage(), 40, 40));

                    int order = i + 1;
                    thumbAction = new ThumbnailAction(icon, thumbnailIcon, "Imagem " + order, order);

                }
                publish(thumbAction);
            }
            // unfortunately we must return something, and only null is valid to
            // return when the return type is void.
            return null;
        }

        /**
         * Process all loaded images.
         */
        @Override
        protected void process(List<ThumbnailAction> chunks) {
            for (ThumbnailAction thumbAction : chunks) {
                JButton thumbButton = new JButton(thumbAction);
                if (thumbAction.getOrder() <= 9) {
                    buttonBar1.add(thumbButton, buttonBar1.getComponentCount() - 1);
                } else {
                    buttonBar2.add(thumbButton, buttonBar2.getComponentCount() - 1);
                }
            }
        }
    };

    /**
     * Creates an ImageIcon if the path is valid.
     * @param String - resource path
     * @param String - description of the file
     */
    protected ImageIcon createImageIcon(Foto foto) {
        if (foto != null) {
            return new ImageIcon(foto.getImagem());
        } else {
            return null;
        }
    }

    /**
     * Resizes an image using a Graphics2D object backed by a BufferedImage.
     * @param srcImg - source image to scale
     * @param w - desired width
     * @param h - desired height
     * @return - the new resized image
     */
    private Image getScaledImage(Image srcImg, int w, int h) {
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }

    /**
     * Action class that shows the image specified in it's constructor.
     */
    private class ThumbnailAction extends AbstractAction {

        /**
         *The icon if the full image we want to display.
         */
        private ImageIcon displayPhoto;
        private ImageIcon resizePhoto;
        private int order;

        /**
         * @param Icon - The full size photo to show in the button.
         * @param Icon - The thumbnail to show in the button.
         * @param String - The descriptioon of the icon.
         */
        public ThumbnailAction(ImageIcon photo, Icon thumb, String desc, int order) {
            displayPhoto = photo;
            resizePhoto = new ImageIcon(getScaledImage(displayPhoto.getImage(), 852, 431));
            this.order = order;
            // The short description becomes the tooltip of a button.
            putValue(SHORT_DESCRIPTION, desc);

            // The LARGE_ICON_KEY is the key for setting the
            // icon when an Action is applied to a button.
            putValue(LARGE_ICON_KEY, thumb);
        }

        /**
         * Shows the full image in the main area and sets the application title.
         */
        @Override
        public void actionPerformed(ActionEvent e) {
            photographLabel.setText(null);
            photographLabel.setIcon(resizePhoto);
        }

        public int getOrder() {
            return order;
        }
    }
    
    private void isShowErrorsLabel(boolean show) {
        dataErrorLabel.setVisible(show);
        valorErrorLabel.setVisible(show);
    }
    
    private void showErrorLabel(UserError userError) {
        userError.getLabel().setVisible(true);
    }
    
    
    
    private Sinistro getSinistro() {
        if (sinistro == null) {
            sinistro = new Sinistro();
        }        
        try {
            sinistro.setData(dataField.getDate());
            sinistro.setObservacao(observacaoField.getText());
            Number valor = valorPrejuizoFormat.getNumberFormat().parseNumber(valorPrejuizoField.getText());
            sinistro.setValorPrejuizo(Double.valueOf(valor.toString()));
        } catch (ParseException ex) {
            sinistro.setValorPrejuizo(0d);
            System.out.println(ex.getMessage());
        }
        
        return sinistro;
    }
    
    private void validateUserInput() throws InputValidationException {
        isShowErrorsLabel(false);
        ErrorsList errorsList = new ErrorsList();
        if(dataField.getDate() == null) {
            errorsList.addError(new UserError(dataErrorLabel, "O data do sinistro não foi informada."));
        }
        if(valorPrejuizoField.getText().isEmpty()) {
            errorsList.addError(new UserError(valorErrorLabel, "O valor do prejuízo não foi informado."));
        }
        if (errorsList.countErrors() > 0) {
            throw new InputValidationException(errorsList);
        }
    }

    @Override
    protected JRootPane createRootPane() {
        ActionListener closeListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cancelarButtonActionPerformed(e);
            }
        };
        KeyStroke stroke =
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane myRootPane = new JRootPane();
        myRootPane.registerKeyboardAction(
                closeListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        return myRootPane;
    }
}
